package com.example.Project.user;

import com.example.Project.user.userHttpMethods.userMethods;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.io.IOException;

public class RegisterController {
    public void start() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("register.fxml"));
        Scene scene = new Scene((Parent)fxmlLoader.load(), 402.0, 479.0);
        Stage stage=new Stage();
        stage.setTitle("社团信息管理-注册");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    @FXML
    private ToggleGroup Sex;

    @FXML
    private Button register;

    @FXML
    private TextField scanfName;

    @FXML
    private PasswordField scanfPass;

    @FXML
    private PasswordField scanfPass2;

    @FXML
    private TextField scanfPhone;

    @FXML
    private RadioButton sex;

    @FXML
    private RadioButton sex1;
    userMethods userMethods=new userMethods();

    @FXML
    void Register(ActionEvent event) throws IOException {
        RadioButton selectedRadioButton = (RadioButton) Sex.getSelectedToggle();
        String gender = selectedRadioButton.getText();
        String name = scanfName.getText();
        String phone = scanfPhone.getText();
        String password1 = scanfPass.getText();
        String password2 = scanfPass2.getText();
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        if(gender!=null&&name!=null&&phone!=null&&password1!=null&&password2!=null){
            if(!password1.equals(password2)){
                alert.setHeaderText("两次密码不一致，请重新填写！！！");
                alert.showAndWait();
                return;
            }else if(!(password1.matches(".*\\d.*") || !password1.matches(".*[a-zA-Z].*")) || !(password1.length() >6)){
                alert.setHeaderText("密码需要包含数字与字母并大于6位，请重新填写！！！");
                alert.showAndWait();
                return;
            }
            int re=userMethods.register(name,password1,gender,phone);
            if(re!=-1){
                alert.setHeaderText("注册成功，你的账号为"+re+",请返回登录界面登录！！！");
                alert.showAndWait();
                return;
            }else{
                alert.setHeaderText("注册失败！！！");
                alert.showAndWait();
                return;
            }
        }else{
            alert.setHeaderText("请将上述必填项填写完整，请补充填写！！！");
            alert.showAndWait();
            return;
        }
    }

}
